//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls
{
	/// <summary>
	/// Summary description for CreateQuickReply.
	/// </summary>
	public class CreateQuickReply : CommunityServer.Controls.TemplatedWebControl
	{
		public CreateQuickReply()
		{
		    Initialize();
		}

	    private void Initialize()
	    {
            

	        CSEvents.AuthorizePost();
    
	        if(context.PostID > 0)
	        {
	            postReplyingTo = Posts.GetPost(context.PostID, context.User.UserID);
	            forum = postReplyingTo.Section as Forum;
	        }
    
    
	        if(postReplyingTo == null)
	        {
	            throw new CSException(CSExceptionType.PostNotFound);
	        }
    
	        // Security check to see if the forum allows anonymous posts
	        //
	        if ( context.User.IsAnonymous && (!forum.EnableAnonymousPosting || !context.SiteSettings.EnableAnonymousUserPosting) ) 
	        {
	            if (!Context.Request.IsAuthenticated) 
	            {
	                Context.Response.Redirect(Globals.GetSiteUrls().Login);
	                Context.Response.End();
	            }
	        }
    
	        Permissions.AccessCheck(forum, Permission.Reply, context.User, postReplyingTo);
    
	        if (postReplyingTo.IsLocked) 
	        {
	            Context.Response.Redirect(Globals.GetSiteUrls().Post(context.PostID));
	            Context.Response.End();
	        }
	    }

	    private Editor postBody;
        private TextBox postSubject;
        private IButton button;
        CSContext context = CSContext.Current;
	    ForumPost postReplyingTo = null;
        Forum forum = null;
        CheckBox reFreshPage = null;

	    protected override void AttachChildControls()
	    {
            Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            

	        postBody = FindControl("PostBody") as Editor;
            postSubject = FindControl("PostSubject") as TextBox;
            button = FindButton("SavePost");
	        reFreshPage = FindControl("RefreshPage") as CheckBox;

            if(reFreshPage != null)
            {
                HttpCookie cookie = Context.Request.Cookies["CSQuickReplyCheck"];
                if(cookie != null && !Globals.IsNullorEmpty(cookie.Value) )
                {
                    reFreshPage.Checked = bool.Parse(cookie.Value);
                }
            }

            if(button != null)
                button.Click +=new EventHandler(button_Click);

            string replyPrePend = ResourceManager.GetString("CreateEditPost_ReplyPrePend");
            if(postReplyingTo.Subject.StartsWith(replyPrePend))
            {
                postSubject.Text = Globals.HtmlDecode(postReplyingTo.Subject);
            }
            else
            {
                postSubject.Text = replyPrePend +  Globals.HtmlDecode(postReplyingTo.Subject);
            }
			
	        Head.AddTitle(Formatter.RemoveHtml("Reply: " + postReplyingTo.Subject,60),Context);
        }

        private void button_Click(object sender, EventArgs e)
        {
            // Only proceed if the post is valid
            //
            if (!Page.IsValid) 
                return;


            Thread postToAdd = new Thread();
            

            if (postReplyingTo.PostID != postReplyingTo.ParentID)
            {
                postToAdd.ParentID = postReplyingTo.ParentID;
            }

            postToAdd.PostLevel = postReplyingTo.PostLevel;
            postToAdd.SectionID = postReplyingTo.SectionID;
            // Get initial post's posting date 
            postToAdd.PostDate = postReplyingTo.PostDate;

            // Get the current StickyDate
            Thread thread = Threads.GetThread(postReplyingTo.ThreadID);
            postToAdd.IsSticky = thread.IsSticky;
            postToAdd.StickyDate = thread.StickyDate;


            postToAdd.Username = context.User.Username;
            postToAdd.Subject = postSubject.Text;
            postToAdd.IsLocked = false;
            postToAdd.IsTracked = true;
            postToAdd.User = context.User;
            postToAdd.IsAnonymousPost = false;
            postToAdd.IsIgnoredPost = context.User.IsIgnored;
            postToAdd.PostType = PostContentType.HTML;
            postToAdd.Body = postBody.Text;

            postToAdd.ParentID = context.PostID;
            Posts.AddPost(postToAdd, context.User);

            //HttpCookie cookie = Context.Request.Cookies["CSQuickReplyCheck"];
            bool isChecked = false;
            if(reFreshPage != null && reFreshPage.Checked)
                isChecked = true;

            HttpCookie cookie = new HttpCookie("CSQuickReplyCheck",isChecked.ToString());
            cookie.Expires = DateTime.Now.AddDays(99);
            Context.Response.Cookies.Add(cookie);

            string returnValue = isChecked ? "true" : "false";

            Modal.ClosePage(this.Page, returnValue);
            
        }
    }
}
